package com.hailian.fintech.hlpayoffserver.service.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.hailian.fintech.hlcomm_interfaceserver.util.HttpUtil;
import com.hailian.fintech.hlcomm_interfaceserver.util.PayCommonUtil;
import com.hailian.fintech.hlcomm_interfaceserver.util.PayConfigUtil;
import com.hailian.fintech.hlcomm_interfaceserver.util.XMLUtil;
import com.hailian.fintech.hlpayoffserver.entity.Weixin_pay;
import com.hailian.fintech.hlpayoffserver.mapper.Weixin_payMapper;
import com.hailian.fintech.hlpayoffserver.service.IWeixin_payService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-05-27
 */
@Service
public class Weixin_payServiceImpl extends ServiceImpl<Weixin_payMapper, Weixin_pay> implements IWeixin_payService {


    /**
     * 微信支付统一下单接口
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    @Override
    public String weixin_pay(String out_trade_no) throws Exception {
        // 账号信息
        String appid = PayConfigUtil.getAppid();  // appid
        //String appsecret = PayConfigUtil.APP_SECRET; // appsecret
        // 商业号
        String mch_id = PayConfigUtil.getMchid();
        // key
        String key = PayConfigUtil.getKey();

        String currTime = PayCommonUtil.getCurrTime();
        String strTime = currTime.substring(8, currTime.length());
        String strRandom = PayCommonUtil.buildRandom(4) + "";
        //随机字符串
        String nonce_str = strTime + strRandom;
        // 价格   注意：价格的单位是分
        //String order_price = "1";
        // 商品名称
        //String body = "企嘉科技商品";

        //查询订单数据表获取订单信息
        //PayOrder payOrder = payOrderDao.get(PayOrder.class,out_trade_no);
        // 获取发起电脑 ip
        String spbill_create_ip = PayConfigUtil.getIP();
        // 回调接口
        String notify_url = PayConfigUtil.NOTIFY_URL;
        String trade_type = "NATIVE";
        String time_start =  new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.DATE, 1);
        String time_expire =  new SimpleDateFormat("yyyyMMddHHmmss").format(ca.getTime());
        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
//        packageParams.put("body",payOrder.getBody());
        packageParams.put("body","可乐");
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", "1");
//        packageParams.put("total_fee", payOrder.getTotalFee());
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);
        packageParams.put("time_start", time_start);
        packageParams.put("time_expire", time_expire);
        String sign = PayCommonUtil.createSign("UTF-8", packageParams,key);
        packageParams.put("sign", sign);

        String requestXML = PayCommonUtil.getRequestXml(packageParams);
        System.out.println("请求xml：：：："+requestXML);

        String resXml = HttpUtil.postData(PayConfigUtil.PAY_API, requestXML);
        System.out.println("返回xml：：：："+resXml);

        Map map = XMLUtil.doXMLParse(resXml);
        //String return_code = (String) map.get("return_code");
        //String prepay_id = (String) map.get("prepay_id");
        String urlCode = (String) map.get("code_url");
        System.out.println("打印调用统一下单接口生成二维码url:::::"+urlCode);
        return urlCode;
    }
    /**
     * 生成二维码图片 不存储 直接以流的形式输出到页面
     * @param content
     * @param response
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public  void encodeQrcode(String content,HttpServletResponse response){
        if(content==null || "".equals(content))
            return;
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300,hints);
            BufferedImage image = toBufferedImage(bitMatrix);
            //输出二维码图片流
            try {
                ImageIO.write(image, "png", response.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (WriterException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    public static BufferedImage toBufferedImage(BitMatrix matrix){
                 int width = matrix.getWidth();
                 int height = matrix.getHeight();
                 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

                 for(int x=0;x<width;x++){
                         for(int y=0;y<height;y++){
                                 image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
                             }
                     }
                 return image;
             }
    /**
     54      * 将LOGO图片放在二维码中间（水印效果）
     55      * 将生成的图片以流的形式输出到页面展示
     56      * @param matrix          BitMatrix
     57      * @param format          图片格式
     58      * @param outStream       输出流
     59      * @param logoPath        LOGO地址
     60      * @param showBottomText  二维码图片底部需要显示的问题
     61      * @throws IOException
     62      */
    /* public static void megerToFile(BitMatrix matrix,String format,OutputStream outStream,String logoPath,String showBottomText) throws IOException {
                 BufferedImage codeImage = toBufferedImage(matrix);
                 Graphics2D gs = codeImage.createGraphics();
                 //1.加入LOGO水印效果
                 if(null != logoPath && !"".equals(logoPath)){
                         //1.1 载入LOGO图片
                         BufferedImage logoImg = ImageIO.read(new File(logoPath));
                         //1.2 考虑到LOGO图片贴到二维码中，建议大小不要超过二维码的1/5;
                         int width = codeImage.getWidth() / LogoPart;
                         int height = codeImage.getHeight() / LogoPart;
                         //1.3 LOGO居中显示
                         int x = (codeImage.getWidth() - width) / 2;
                         int y = (codeImage.getHeight() - height) / 2;
                         gs.drawImage(logoImg, x, y, logoImg.getWidth(), logoImg.getHeight(), null);
                         logoImg.flush();
                     }
                 //画底部画布放置二维码及底部文字
                 int backAmageW = matrix.getWidth();
                 int backAmageH = matrix.getHeight()+50;
                 BufferedImage backAmage = new BufferedImage(backAmageW, backAmageH, BufferedImage.TYPE_3BYTE_BGR);
                 for(int x=0;x<backAmageW;x++){
                         for(int y=0;y<backAmageH;y++){
                                 backAmage.setRGB(x, y, WHITE);
                             }
                     }
                 Graphics2D backgs = backAmage.createGraphics();
                 backgs.drawImage(codeImage, 0, 0, codeImage.getWidth(), codeImage.getHeight(), null);
                 gs.dispose();
                 codeImage.flush();
                 //2.二维码图片底部插入文字
                 if(null != showBottomText && !"".equals(showBottomText)){
                         //2.1 设置字体样式
                         Font font = new Font("微软雅黑", Font.PLAIN, 16);
                         backgs.setColor(Color.black);
                         backgs.setFont(font);
                         //2.2 字体显示位置
                         int x = (backAmage.getWidth() - getWatermarkLength(showBottomText, gs))/2-10;
                         int y = backAmage.getHeight()-30;
                         backgs.drawString(showBottomText, x, y);
                     }
                 backgs.dispose();
                 backAmage.flush();
                 ImageIO.write(backAmage, format, outStream);
             }*/

    @Override
    /**
     * 微信支付回调方法
     * @param request
     * @param response
     * @throws Exception
     */
    public void weixin_notify(HttpServletRequest request,HttpServletResponse response) throws Exception{

        //读取参数
        InputStream inputStream ;
        StringBuffer sb = new StringBuffer();
        inputStream = request.getInputStream();
        String s ;
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        while ((s = in.readLine()) != null){
            sb.append(s);
        }
        in.close();
        inputStream.close();

        //解析xml成map
        Map<String, String> m = new HashMap<String, String>();
        m = XMLUtil.doXMLParse(sb.toString());

        //过滤空 设置 TreeMap
        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
        Iterator it = m.keySet().iterator();
        while (it.hasNext()) {
            String parameter = (String) it.next();
            String parameterValue = m.get(parameter);

            String v = "";
            if(null != parameterValue) {
                v = parameterValue.trim();
            }
            packageParams.put(parameter, v);
        }

        // 账号信息
        String key = PayConfigUtil.getKey(); // key
        String out_trade_no = (String)packageParams.get("out_trade_no");
        //logger.info(packageParams);
        //判断签名是否正确
        if(PayCommonUtil.isTenpaySign("UTF-8", packageParams,key)) {
            //------------------------------
            //处理业务开始
            //------------------------------
            String resXml = "";
            if("SUCCESS".equals((String)packageParams.get("result_code"))){
                // 这里是支付成功
                //////////执行自己的业务逻辑////////////////
                String mch_id = (String)packageParams.get("mch_id");
                String openid = (String)packageParams.get("openid");
                String is_subscribe = (String)packageParams.get("is_subscribe");

                String bank_type = (String)packageParams.get("bank_type");
                String total_fee = (String)packageParams.get("total_fee");
                String transaction_id = (String)packageParams.get("transaction_id");

                System.out.println("mch_id:"+mch_id);
                System.out.println("openid:"+openid);
                System.out.println("is_subscribe:"+is_subscribe);
                System.out.println("out_trade_no:"+out_trade_no);
                System.out.println("total_fee:"+total_fee);
                System.out.println("bank_type:"+bank_type);
                System.out.println("transaction_id:"+transaction_id);

                //成功回调后需要处理预生成订单的状态和一些支付信息
                //查询数据库中订单，首先判定订单中金额与返回的金额是否相等，不等金额被纂改
                //判定订单是否已经被支付，不可重复支付
                //正常处理相关业务逻辑

            } else {
                System.out.println("支付失败,错误信息：" + packageParams.get("err_code")+
                        "-----订单号：：："+out_trade_no+"*******支付失败时间：：：："
                        +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

                String err_code = (String)packageParams.get("err_code");

                resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
                        + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";

            }
            //------------------------------
            //处理业务完毕
            //------------------------------
            BufferedOutputStream out = new BufferedOutputStream(
                    response.getOutputStream());
            out.write(resXml.getBytes());
            out.flush();
            out.close();
        } else{
            System.out.println("通知签名验证失败---时间::::"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }

    }


}
